package com.eureka.eurekaserviceinvoker.service;

import com.eureka.eurekaserviceinvoker.entity.Person;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * Created by earl on 2018/5/2.
 */
@Component
//使用DefaultProperties减少@HystrixCommand的代码量
@DefaultProperties(groupKey = "PersonDefaultKey" )
public class PersonDefaultService {

    @Autowired
    private RestTemplate restTemplate;


    //这里的gorupkey属性，默认使用@DefaultProperties 设置的属性
    @HystrixCommand (fallbackMethod = "getPersonFallback")
    public Person getDefaultPerson(Long id) {
        Person p = restTemplate.getForObject("http://eureka-service-provider/provider/person/{id}", Person.class, id);
        return p;
    }

    //回退方法，这里的参数、参数类型要和getPerson一致
    public Person getPersonFallback(Long id){
        Person p = new Person();
        p.setId(0L);
        p.setName(" fallBack Name ");
        return p;
    }

    //缓存注解,必须配合@HystrixCommand、HystrixFilter使用
    @CacheResult
    @HystrixCommand
    public Person getCachePerson(Long id) {
        System.out.println("-----getCachePerson execute----");
        Person p = restTemplate.getForObject("http://eureka-service-provider/provider/person/{id}", Person.class, id);
        return p;
    }









}
